package nicg.st.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import nicg.st.entity.IPParseBean;
import nicg.st.entity.IPParseBean;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlParseIp {
	private static List<IPParseBean> IPParseBeanList = null;
	private static IPParseBean IPParseBean = null;
	
	public static List<IPParseBean> getIPParseBeans(File file){
		
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(file);
			Element IPParseBeanstore = document.getRootElement();
			Iterator storeit = IPParseBeanstore.elementIterator();
			
			IPParseBeanList = new ArrayList<IPParseBean>();
			while(storeit.hasNext()){
				
				IPParseBean = new IPParseBean();
				Element IPParseBeanElement = (Element) storeit.next();
				
				
				Iterator IPParseBeanit = IPParseBeanElement.elementIterator();
				while(IPParseBeanit.hasNext()){
					Element child = (Element) IPParseBeanit.next();
					String nodeName = child.getName();
					if(nodeName.equals("name")){
						String name = child.getStringValue();
						IPParseBean.setName(name);
					}else if(nodeName.equals("ip")){
						String ip = child.getStringValue();
						IPParseBean.setIP(ip);
					}
				}
				IPParseBeanList.add(IPParseBean);
				IPParseBean = null;
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return IPParseBeanList;
}
}
